<?php
/**
 * Created by PhpStorm.
 * User: coolinmind
 * Date: 28/11/2018
 * Time: 11:35
 */

namespace App\Services\Mobile\Bill;


use App\Models\Agent\Agent;
use App\Models\Finance\Income;
use App\Models\Finance\IncomeExcel;
use DB;

class BillLogic
{
    //账单列表
    public function billList($data)
    {
        $_where = 'WHERE agent_id = ' . $data['agent_id'];

        if (isset($data['year']) && $data['year']) {
            $_where = 'WHERE agent_id = ' . $data['agent_id'].' && year = '.$data['year'];
            $selected = $data['year'];
        }else{
            $selected = date('Y');
        }

        $sql = 'SELECT SUM(e.agent_money) total, i.year, i.month FROM `income_excel` e RIGHT JOIN `income` i ON e.income_id = i.id ' . $_where . ' GROUP BY year, month';
        $list = DB::select($sql);
        foreach ($list as &$v) {
            //获取income表相关ids
            $idsData = Income::with(['incomeExcel' => function ($query) use($data) {
                $query->where(['agent_id' => $data['agent_id']]);
            }])->where(['year' => $v->year, 'month' => $v->month])->get()->toArray();
            $v->ids = json_encode(array_column($idsData, 'id'));
        }
        $arr = [
            'list' => $list,
            'selected' => $selected
        ];
        return auto_return($arr);
    }

    //账单详情
    public function billDetail($data)
    {
        $mixtureData = json_decode($data['data']);
        if (is_object($mixtureData)) {
            $idStr = json_decode($mixtureData->ids);
        }else{
            throw_exception('参数错误,请返回重试', $mixtureData);
        }
        $thisMonth = $mixtureData->total; //本月收入
        $agentId = $data['agent_id']; //代理id

        //总收入
        $totalIncome = IncomeExcel::where(['agent_id' => $agentId])->sum('agent_money');

        $userInfo = Agent::with('users_info')->where(['id' => $agentId])->first()->toArray();
        $userInfo['total_income'] = $totalIncome;
        $userInfo['this_month_income'] = $thisMonth;

        $thisMonthUser = Agent::with(['users_info' => function ($query) use ($agentId) {
            $query->where(['check_time' => date('Y-m-d')]);
        }])->where(['id' => $agentId])->first()->toArray();

        $userNum = count($thisMonthUser['users_info']); //本月新增代理人数

        //单用户最高收入
        $maxIncome = IncomeExcel::where(['agent_id' => $agentId])->whereIn('income_id', $idStr)->max('agent_money');

        //单用户最低收入
        $minIncome = IncomeExcel::where(['agent_id' => $agentId])->whereIn('income_id', $idStr)->min('agent_money');

        //代理用户列表
        $userList = IncomeExcel::where(['agent_id' => $agentId])->whereIn('income_id', $idStr)->get()->toArray();

        //获取柱状图数据
        $zeroToTen = IncomeExcel::where(['agent_id' => $agentId])->whereIn('income_id', $idStr)->whereBetween('agent_money', [0, 10])->count();
        $tenToFifty = IncomeExcel::where(['agent_id' => $agentId])->whereIn('income_id', $idStr)->whereBetween('agent_money', [10, 50])->count();
        $fiftyToTwoHundred = IncomeExcel::where(['agent_id' => $agentId])->whereIn('income_id', $idStr)->whereBetween('agent_money', [50, 200])->count();
        $twoHundredToFiveHundred = IncomeExcel::where(['agent_id' => $agentId])->whereIn('income_id', $idStr)->whereBetween('agent_money', [200, 500])->count();
        $fiveHundredToTwoThousand = IncomeExcel::where(['agent_id' => $agentId])->whereIn('income_id', $idStr)->whereBetween('agent_money', [500, 2000])->count();
        $moreThanTwoThousand = IncomeExcel::where(['agent_id' => $agentId])->whereIn('income_id', $idStr)->where('agent_money', '>', 2000)->count();
        $echartData = [
            $zeroToTen,
            $tenToFifty,
            $fiftyToTwoHundred,
            $twoHundredToFiveHundred,
            $fiveHundredToTwoThousand,
            $moreThanTwoThousand,
        ];

        $userInfo['max_income'] = $maxIncome;
        $userInfo['min_income'] = $minIncome;
        $userInfo['user_num'] = $userNum;
        $userInfo['year'] = $mixtureData->year;
        $userInfo['month'] = $mixtureData->month;

        $userInfo['echart_data'] = json_encode($echartData);
        $userInfo['user_list'] = $userList;
        return auto_success($userInfo);
    }
}